package com.example.handlerdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var handler1 = object : Handler(Looper.getMainLooper()) {
        override fun dispatchMessage(msg: Message) {
            Log.v("handler1",msg.data.getString("name")+"-1")
        }
    }

    var handler2 = object : Handler(Looper.getMainLooper()) {
        override fun dispatchMessage(msg: Message) {
            Log.v("handler2",msg.data.getString("name")+"-2")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button1.setOnClickListener {
            val message1 = Message.obtain()
            message1.data.putString("name","jack")
            handler1.sendMessage(message1)
            val message2 = Message.obtain()
            message2.data.putString("name","jerry")
            handler2.sendMessage(message2)
//            handler.post()
        }
    }
}